<template>
  <div class="xtx-goods-page" v-if="goodsInfo">
    <div class="container">
      <!-- 面包屑
      1:数据问题（定义api调用获取数据）
      2：渲染问题（拿到数据存储并渲染）
      3:使用watch解决商品切换问题      
       -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/' + goodsInfo.categories[1].id">{{
          goodsInfo.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="'/category/sub/' + goodsInfo.categories[0].id">{{
          goodsInfo.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goodsInfo.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <!-- 图片展示区域
        1：创建组件，加入html代码
        2：在商品详情页面使用该组件
        3：父传子商品详情图片数据，组件渲染
        4：完成逻辑   

        放大镜
        布局:
           1：遮罩层
           2：大图
           import { useMouseInElement } from '@vueuse/core'
          const {isOutside}= useMouseInElement(dom)
          isOutside:移出true 移入false
        1：移入显示移出消失
        2：遮罩层随鼠标移动
        3：鼠标在遮罩层中间
        4：移动时有范围限制
        5：右边放大镜区域随鼠标移动 
         -->
          <GoodsImage :images="goodsInfo.mainPictures"></GoodsImage>
          <!-- 销售属性
          1:创建组件
          2：在详情组件中使用
          3：通过父子传值获取详情数据后在销售组件中使用渲染
           -->
          <GoodsSales :goodsInfo="goodsInfo"></GoodsSales>
        </div>
        <div class="spec">
          <!-- 商品基本信息 -->
          <GoodName :goodsInfo="goodsInfo"></GoodName>
          <!-- u商品属性展示sku
          1:封装组件
          2：使用
          3：渲染
          4：点击功能（选择）
          
           -->
          <GoodsSku :goodsInfo="goodsInfo" @change="skuChange"></GoodsSku>
          <!-- 数量组件 
          v-model:
             子组件标签  modelValue   @update:modelValue
          -->
          <XtxNumbox
            v-model="goodNum"
            :min="1"
            :max="goodsInfo.inventory"
            v-if="numBol"
          ></XtxNumbox>
          <!-- 加入购物车按钮 -->
          <XtxButton type="primary" size="large" style="marginTop:20px"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs :goodsInfo="goodsInfo"></GoodsTabs>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn"></div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot type="1"></GoodsHot>
          <GoodsHot type="2"></GoodsHot>
          <GoodsHot type="3"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant'
import { goods } from '@/api/product'
import { useRoute, onBeforeRouteLeave } from 'vue-router'
import { ref, watch, nextTick, provide } from 'vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
//   获取商品详情
function getGoodsInfo () {
  const route = useRoute() // route===this.$route
  const goodsInfo = ref('')
  const stop = watch(
    () => route.params.id,
    () => {
      // 调用api获取商品详情
      goodsInfo.value = ''
      goods(route.params.id).then(res => {
        goodsInfo.value = res.result
        console.log('商品详情', res)
      })
      // nextTick(() => {
      //   console.log(1)
      // })
    },
    {
      immediate: true
    }
  )
  onBeforeRouteLeave(() => {
    //   停止watch侦听
    stop()
  })
  return goodsInfo
}
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodName,
    GoodsSku,
    GoodsRelevant,
    GoodsTabs,
    GoodsHot
  },
  setup () {
    //   获取商品详情
    const goodsInfo = getGoodsInfo()
    provide('goods', goodsInfo)
    // sku选择后的change事件
    function skuChange (obj) {
      goodsInfo.value.price = obj.price
      goodsInfo.value.oldPrice = obj.oldPrice
      goodsInfo.value.inventory = obj.inventory
      // 让数量回到初始状态
      numBol.value = false
      goodNum.value = 1
      nextTick(() => {
        numBol.value = true
      })
      console.log(obj)
    }
    // 商品数据
    const goodNum = ref(1)
    const numBol = ref(true)

    return { goodsInfo, skuChange, goodNum, numBol }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
